﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SIBI.Helpers
{
    
    public class ReportData
    {
        public ReportData()
        {
            this.ReportParameters = new List<Parameter>();
            this.DataParameters = new List<Parameter>();
        }

        public bool IsLocal { get; set; }
        public string ReportName { get; set; }
        public List<Parameter> ReportParameters { get; set; }
        public List<Parameter> DataParameters { get; set; }
    }

    public class Parameter
    {
        public string ParameterName { get; set; }
        public string Value { get; set; }
    }
    public class MReportes : System.Web.UI.Page
    {
        protected ReportData ReportDataObj { get; set; }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (HttpContext.Current != null)
                if (HttpContext.Current.Session["ReportData"] != null)
                {
                    ReportDataObj = HttpContext.Current.Session["ReportData"] as ReportData;
                    return;
                }
            ReportDataObj = new ReportData();
            CaptureRouteData(Page.Request);
        }


        private void CaptureRouteData(HttpRequest request)
        {
            var mode = (request.QueryString["rptmode"] + "").Trim();
            ReportDataObj.IsLocal = mode == "local" ? true : false;
            ReportDataObj.ReportName = request.QueryString["reportname"] + "";
            string dquerystr = request.QueryString["parameters"] + "";
            if (!String.IsNullOrEmpty(dquerystr.Trim()))
            {
                var param1 = dquerystr.Split(',');
                foreach (string pm in param1)
                {
                    var rp = new Parameter();
                    var kd = pm.Split('=');
                    if (kd[0].Substring(0, 2) == "rp")
                    {
                        rp.ParameterName = kd[0].Replace("rp", "");
                        if (kd.Length > 1) rp.Value = kd[1];
                        ReportDataObj.ReportParameters.Add(rp);
                    }
                    else if (kd[0].Substring(0, 2) == "dp")
                    {
                        rp.ParameterName = kd[0].Replace("dp", "");
                        if (kd.Length > 1) rp.Value = kd[1];
                        ReportDataObj.DataParameters.Add(rp);
                    }
                }
            }
        }
        public static void Modal_Show(System.Web.UI.Control tcobj, string tcselector)
        {
            tcobj.Page.ClientScript.RegisterClientScriptBlock(tcobj.GetType(), tcselector,
               @"<script type='text/javascript'>
                $(document).ready(function () {
                    $('" + tcselector + @"').modal({ opacity: 10,
                        minHeight:580,maxHeight:590, minWidth: 780,maxWidth: 790,
                        autoResize: true, modal: true,autoPosition:true,close: true,overlayClose: true,
                        closeHTML: ""<a class='modalCloseImg' title='Cerrar' ></a>""
                    });
                });
                </script>
               ");
        }
    }
}